package com.mall.controller;

import com.mall.common.AuthCheck;
import com.mall.common.Result;
import com.mall.dto.UserLoginDTO;
import com.mall.dto.UserRegisterDTO;
import com.mall.entity.User;
import com.mall.service.UserService;
import com.mall.util.JwtUtil;
import com.mall.util.TokenUtils;
import com.mall.vo.UserInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 用户控制器
 * 
 * @author Mall Team
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    
    @Autowired
    private UserService userService;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private TokenUtils tokenUtils;
    
    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result<String> register(@Valid @RequestBody UserRegisterDTO registerDTO) {
        userService.register(registerDTO);
        return Result.success("注册成功");
    }
    
    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<UserInfoVO.LoginVO> login(@Valid @RequestBody UserLoginDTO loginDTO) {
        UserInfoVO.LoginVO loginVO = userService.login(loginDTO);
        return Result.success("登录成功", loginVO);
    }
    
    /**
     * 获取当前用户信息
     */
    @GetMapping("/info")
    @AuthCheck
    public Result<UserInfoVO> getUserInfo() {
        // TODO: 从token中解析用户ID
        String token = tokenUtils.getTokenFromRequest();
        Long userId = jwtUtil.getUserIdFromToken(token);
        UserInfoVO userInfo = userService.getUserInfo(userId);
        return Result.success(userInfo);
    }
    
    /**
     * 更新用户信息
     */
    @PutMapping("/info")
    @AuthCheck
    public Result<String> updateUserInfo(@RequestBody User user) {
        // TODO: 从token中解析用户ID
        String token = tokenUtils.getTokenFromRequest();
        Long userId = jwtUtil.getUserIdFromToken(token);
        userService.updateUserInfo(userId, user);
        return Result.success("更新成功");
    }
    
    /**
     * 修改密码
     */
    @PostMapping("/changePassword")
    @AuthCheck
    public Result<String> changePassword(@RequestParam String oldPassword,
                                      @RequestParam String newPassword) {
        // TODO: 从token中解析用户ID
        String token = tokenUtils.getTokenFromRequest();
        Long userId = jwtUtil.getUserIdFromToken(token);
        userService.changePassword(userId, oldPassword, newPassword);
        return Result.success("密码修改成功");
    }
    
    /**
     * 检查用户名是否存在
     */
    @GetMapping("/checkUsername")
    public Result<Boolean> checkUsername(@RequestParam String username) {
        boolean exists = userService.existsByUsername(username);
        return Result.success(exists);
    }
    
    /**
     * 检查手机号是否存在
     */
    @GetMapping("/checkPhone")
    public Result<Boolean> checkPhone(@RequestParam String phone) {
        boolean exists = userService.existsByPhone(phone);
        return Result.success(exists);
    }
    
    /**
     * 检查邮箱是否存在
     */
    @GetMapping("/checkEmail")
    public Result<Boolean> checkEmail(@RequestParam String email) {
        boolean exists = userService.existsByEmail(email);
        return Result.success(exists);
    }
}